package com.ymatou.iapi.login.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.login.parameter.WeiXinUserRequestDtoBean;
import com.ymatou.iapi.login.parameter.WxUserLoginBean;
import com.ymatou.iapi.login.service.WxUserLoginCall;
import com.ymatou.iapi.userservice.parameter.enums.ThirdPartyTypeEnum;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.database.model.LoginLog;
import com.ymttest.database.model.ThirdpartyUserbind;
import com.ymttest.database.model.ThirdpartyUserbindHistory;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.constant.ECode;

/**
 * 直播微店买家登入 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_WxUserLogin {
	private static WxUserLoginBean wxuserloginBean;
	private static WxUserLoginCall wxuserloginCall;
	private static  JUserWapper ju ;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("直播微店买家登入");
	}

	@Before
	public void caseUp() {
		wxuserloginBean = new WxUserLoginBean();
		wxuserloginCall = new WxUserLoginCall();
		ju= new JUserWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(wxuserloginCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private WxUserLoginBean initWxuserloginBean() {
		WxUserLoginBean wx = new WxUserLoginBean();
		wx.setAppSource(20);
		wx.setCity("Shanghai");
		wx.setClientIp(TestDataManager.getRandomIp());
		wx.setCountry("China");
		wx.setDeviceId(TestDataManager.getRandomUUID().toString());
		wx.setDid(TestDataManager.getRandomUUID().toString());
		wx.setHeadImgUrl("http://img.ymatou.com/app/flag/circle/France.png");
		wx.setNickName("nk" + YMTDateUtil.getSysTime("MMddHHmmssSSS"));
		wx.setOpenId(TestDataManager.getRandomUUID().toString().replaceAll("-", "").substring(0, 30));
		wx.setProvince("Shanghai");
		wx.setTerminalSource(2);
		wx.setUnionId(TestDataManager.getRandomUUID().toString().replaceAll("-", "").substring(0, 30));
		return wx;
	}

	private void verifyDBBind(Integer uid, WxUserLoginBean cq) {
		Logger.comment("验证thirdparty_userbind");
		ThirdpartyUserbind thirdinfo = ju.selectthirdpartyuserbind(uid).get(0);
		Logger.verifyEquals(cq.getUnionId(), thirdinfo.getThirdPartyId(), "验证ThirdPartyId");
		Logger.verifyEquals(ThirdPartyTypeEnum.WeiXin.getCode(), thirdinfo.getThirdPartyType(), "验证ThirdPartyType");
		
		
	}

	private void verifyDB(Integer uid, WxUserLoginBean cq) throws InterruptedException {
		Logger.comment("验证thirdparty_userbind");
		ThirdpartyUserbind thirdinfo = ju.selectthirdpartyuserbind(uid).get(0);
		Logger.verifyEquals(cq.getUnionId(), thirdinfo.getThirdPartyId(), "验证ThirdPartyId");
		Logger.verifyEquals(ThirdPartyTypeEnum.WeiXin.getCode(), thirdinfo.getThirdPartyType(), "验证ThirdPartyType");

		Logger.comment("验证thirdparty_userbind_history");
		ThirdpartyUserbindHistory thirdinfohist = ju.selectthirdpartyuserbindhistory(uid).get(0);
		Logger.verifyEquals(true, thirdinfohist.getBinding(), "验证binding状态");
		Logger.verifyEquals(cq.getAppSource(), thirdinfohist.getAppSource(), "验证AppSource");
		Logger.verifyEquals(cq.getClientIp(), thirdinfohist.getClientIp(), "验证ClientIp");
		Logger.verifyEquals(cq.getDeviceId(), thirdinfohist.getDeviceId(), "验证DeviceId");
		Logger.verifyEquals(cq.getDid(), thirdinfohist.getEquimentid(), "验证Equimentid");
		Logger.verifyEquals(cq.getTerminalSource(), thirdinfohist.getTerminalSource(), "验证TerminalSource");

		Logger.verifyEquals(cq.getCity(), thirdinfohist.getCity(), "验证City");
		Logger.verifyEquals(cq.getProvince(), thirdinfohist.getProvince(), "验证City");
		Logger.verifyEquals(cq.getCountry(), thirdinfohist.getCountry(), "验证Country");

		Logger.verifyEquals(cq.getOpenId(), thirdinfohist.getOpenId(), "验证OpenId");
		Logger.verifyEquals(cq.getHeadImgUrl(), thirdinfohist.getHeadimgurl(), "验证Headimgurl");
		
		LoginLog loginLog = new JUserWapper().selectLastLoginLogByUid(uid).get(0);
		Logger.verifyEquals(wxuserloginCall.getJsonObject().get("data").getAsJsonObject().get("loginId").getAsString(), loginLog.getLoginId(), "验证LoginLog表中LoginId");
		Logger.verifyEquals(cq.getClientIp(), loginLog.getClientIp(), "验证LoginLog表中ClientIp");
		// wx登入6
		Logger.verifyEquals(6, loginLog.getChannelSource(), "验证LoginLog表中ChannelSource");
		Logger.verifyEquals(wxuserloginBean.getTerminalSource(), loginLog.getTerminalSource(), "验证LoginLog表中TerminalSource");
		Logger.verifyEquals(cq.getDeviceId(), loginLog.getDeviceId(), "验证LoginLog表中DeviceId");
		Logger.verifyNotNull(loginLog.getServerIP(), "验证LoginLog表中ServerIP");

		if (wxuserloginCall.getString("code").equals("200")) {
			Logger.verifyIsContains("成功", loginLog.getResponseDesc(), "验证LoginLog表中ResponseDesc");
			Logger.verifyEquals(200, loginLog.getResponseCode(), "验证LoginLog表中ResponseCode");
		} 
		Logger.verifyEquals(1, loginLog.getUserSystem(), "验证LoginLog表中UserSystem");
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_WxUserLogin_001() {
		Logger.start(true, "首次直播微店买家登入成功");
		try {
			wxuserloginBean = initWxuserloginBean();
			wxuserloginCall.setData(wxuserloginBean);
			wxuserloginCall.callService();
			Logger.verifyEquals(ECode.SUCCESS, wxuserloginCall.getString("code"), "验证code");
			Integer uid = wxuserloginCall.getJsonObject().get("data").getAsJsonObject().get("userId").getAsInt();
			Logger.verifyEquals(wxuserloginBean.getNickName(),  wxuserloginCall.getJsonObject().get("data").getAsJsonObject().get("loginId").getAsString(), "验证loginId");
			verifyDB(uid, wxuserloginBean);
			verifyDBBind(uid, wxuserloginBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_WxUserLogin_002() {
		Logger.start(true, "不输入unionId失败");
		try {
			wxuserloginBean = initWxuserloginBean();
			wxuserloginBean.setUnionId(null);
			wxuserloginCall.setData(wxuserloginBean);
			wxuserloginCall.callService();
			Logger.verifyEquals("400", wxuserloginCall.getString("code"), "验证code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_WxUserLogin_003() {
		Logger.start(true, "不输入openid,失败");
		try {
			wxuserloginBean = initWxuserloginBean();
			wxuserloginBean.setOpenId(null);
			wxuserloginCall.setData(wxuserloginBean);
			wxuserloginCall.callService();
			Logger.verifyEquals("400", wxuserloginCall.getString("code"), "验证code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_WxUserLogin_004() {
		Logger.start(true, "不输入terminalSource,失败");
		try {
			wxuserloginBean = initWxuserloginBean();
			wxuserloginBean.setTerminalSource(null);
			wxuserloginCall.setData(wxuserloginBean);
			wxuserloginCall.callService();
			Logger.verifyEquals("400", wxuserloginCall.getString("code"), "验证code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_WxUserLogin_005() {
		Logger.start(true, "不输入clientIp,失败");
		try {
			wxuserloginBean = initWxuserloginBean();
			wxuserloginBean.setClientIp(null);
			wxuserloginCall.setData(wxuserloginBean);
			wxuserloginCall.callService();
			Logger.verifyEquals("400", wxuserloginCall.getString("code"), "验证code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_WxUserLogin_006() {
		Logger.start(true, "不输入appSource,失败");
		try {
			wxuserloginBean = initWxuserloginBean();
			wxuserloginBean.setAppSource(null);
			wxuserloginCall.setData(wxuserloginBean);
			wxuserloginCall.callService();
			Logger.verifyEquals("400", wxuserloginCall.getString("code"), "验证code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_WxUserLogin_007() {
		Logger.start(true, "非法微信昵称,自动生成登入名");
		try {
			wxuserloginBean = initWxuserloginBean();
			wxuserloginBean.setNickName("ymt0000");
			wxuserloginCall.setData(wxuserloginBean);
			wxuserloginCall.callService();
			Logger.verifyEquals("5001", wxuserloginCall.getString("code"), "验证code");
			Logger.verifyIsContains("不合法的字符", wxuserloginCall.getString("message"), "验证message");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_WxUserLogin_008() {
		Logger.start(true, "重复微信昵称,自动生成登入名");
		try {
			wxuserloginBean = initWxuserloginBean();
			wxuserloginBean.setNickName("nk0319105810838");
			wxuserloginCall.setData(wxuserloginBean);
			wxuserloginCall.callService();
			Logger.verifyEquals(ECode.SUCCESS, wxuserloginCall.getString("code"), "验证code");
			Integer uid = wxuserloginCall.getJsonObject().get("data").getAsJsonObject().get("userId").getAsInt();
			Logger.verifyNotEquals( "nk0319105810838",wxuserloginCall.getJsonObject().get("data").getAsJsonObject().get("loginId").getAsString(), "验证loginId");
			verifyDB(uid, wxuserloginBean);
			verifyDBBind(uid, wxuserloginBean);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_WxUserLogin_009() {
		Logger.start(true, "已注册直播微店买家再次登入成功");
		try {
			wxuserloginBean = initWxuserloginBean();
			wxuserloginCall.setData(wxuserloginBean);
			wxuserloginCall.callService();
			wxuserloginCall.callService();
			Logger.verifyEquals(ECode.SUCCESS, wxuserloginCall.getString("code"), "验证code");
			Integer uid = wxuserloginCall.getJsonObject().get("data").getAsJsonObject().get("userId").getAsInt();
			Logger.verifyEquals(wxuserloginBean.getNickName(),  wxuserloginCall.getJsonObject().get("data").getAsJsonObject().get("loginId").getAsString(), "验证loginId");
			verifyDB(uid, wxuserloginBean);
			verifyDBBind(uid, wxuserloginBean);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_WxUserLogin_010() {
		Logger.start(true, "已注册直播微店买家使用不同渠道再次登入成功(openid,appSource变化,增加绑定记录)");
		try {
			wxuserloginBean = initWxuserloginBean();
			wxuserloginCall.setData(wxuserloginBean);
			wxuserloginCall.callService();
			wxuserloginBean.setOpenId(TestDataManager.getRandomUUID().toString().replaceAll("-", "").substring(0, 30));
			wxuserloginBean.setAppSource(1);
			wxuserloginCall.callService();
			Logger.verifyEquals(ECode.SUCCESS, wxuserloginCall.getString("code"), "验证code");
			Integer uid = wxuserloginCall.getJsonObject().get("data").getAsJsonObject().get("userId").getAsInt();
			Logger.verifyEquals(wxuserloginBean.getNickName(),  wxuserloginCall.getJsonObject().get("data").getAsJsonObject().get("loginId").getAsString(), "验证loginId");
			verifyDB(uid, wxuserloginBean);
			verifyDBBind(uid, wxuserloginBean);
			Logger.verifyEquals(2,ju.selectthirdpartyuserbind(uid).size(),"绑定了2条");
			Logger.verifyEquals(2,ju.selectthirdpartyuserbindhistory(uid).size(),"绑定记录2条");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_WxUserLogin_011() {
		Logger.start(false, "openid过长,失败");
		try {
			wxuserloginBean = initWxuserloginBean();
			wxuserloginBean.setOpenId(TestDataManager.getRandomUUID().toString());
			wxuserloginCall.setData(wxuserloginBean);
			wxuserloginCall.callService();
			Logger.verifyEquals("400", wxuserloginCall.getString("code"), "验证code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_WxUserLogin_012() {
		Logger.start(false, "UnionId过长,失败");
		try {
			wxuserloginBean = initWxuserloginBean();
			wxuserloginBean.setUnionId(TestDataManager.getRandomUUID().toString());
			wxuserloginCall.setData(wxuserloginBean);
			wxuserloginCall.callService();
			Logger.verifyEquals("400", wxuserloginCall.getString("code"), "验证code");


		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}